// Copyright (c) 2022 NetEase, Inc. All rights reserved.
// Use of this source code is governed by a MIT license that can be
// found in the LICENSE file.

import 'package:nim_core/nim_core.dart';
import 'package:hawk/hawk.dart';

import '../main_test.dart';
import 'method_name_value.dart';

class HandleAudioRecorderCase extends HandleBaseCase {
  HandleAudioRecorderCase();

  @override
  Future<ResultBean?> handle(event) async {
    super.handle(event);

    var isCallback = false;

    if (className?.trim() != 'AudioService') {
      return null;
    }

    var inputParams = Map<String, dynamic>();
    if (params != null && params!.length > 0) {
      inputParams = params![0] as Map<String, dynamic>;
    }

    switch (methodName?.trim()) {
      case startRecord:
        {
          var dur = inputParams['maxDuration'] as int;
          var type = (inputParams['recordType'] as String) == 'AAC'
              ? AudioOutputFormat.AAC
              : AudioOutputFormat.AMR;
          NIMResult<bool> ret =
              await NimCore.instance.audioService.startRecord(type, dur);
          handleCaseResult =
              ResultBean(code: ret.code, data: ret.data, message: methodName);
          break;
        }
      case stopRecord:
        {
          NIMResult<bool> ret =
              await NimCore.instance.audioService.stopRecord();
          handleCaseResult =
              ResultBean(code: ret.code, data: ret.data, message: methodName);
          break;
        }
      case cancelRecord:
        {
          NIMResult<bool> ret =
              await NimCore.instance.audioService.cancelRecord();
          handleCaseResult =
              ResultBean(code: ret.code, data: ret.data, message: methodName);
          break;
        }
      case isAudioRecording:
        {
          NIMResult<bool> ret =
              await NimCore.instance.audioService.isAudioRecording();
          handleCaseResult =
              ResultBean(code: ret.code, data: ret.data, message: methodName);
          break;
        }
      case getAmplitude:
        {
          NIMResult<int> ret =
              await NimCore.instance.audioService.getAmplitude();
          handleCaseResult =
              ResultBean(code: ret.code, data: ret.data, message: methodName);
          break;
        }
      case 'onAudioRecordStatus':
        {
          isCallback = true;
          subsriptions.add(
              NimCore.instance.audioService.onAudioRecordStatus.listen((event) {
            print(
                'AudioService#onAudioRecordStatus ${recordInfoToJson(event)}');
            IntegratedManager.instance.report(
                'onAudioRecordStatus',
                ResultBean(
                    code: 0,
                    data: recordInfoToJson(event),
                    message: methodName));
          }));
          break;
        }
    }
    if (isCallback) {
      handleCaseResult = ResultBean.success(data: null);
    }
    return handleCaseResult;
  }

  Map<String, dynamic> recordInfoToJson(RecordInfo info) {
    return <String, dynamic>{
      'filePath': info.filePath,
      'fileSize': info.fileSize,
      'duration': info.duration,
      'maxDuration': info.duration,
      'recordState': info.recordState.toString(),
      'recordType': info.recordType.toString()
    };
  }
}
